package com.example.hfut_edu;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class page_settings extends AppCompatActivity {
    public static page_settings settings_obj = null;
    TextView textView2,textView9;
    WebView webview_settings;
    Button btn_denglu_jiaowu,btn_denglu_xxmh,btn_tuichu_jiaowu,btn_tuichu_xxmh;

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_settings);
        settings_obj= this;

        webview_settings = findViewById(R.id.webview_settings);
        WebSettings webSettings = webview_settings.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webview_settings.getSettings().setDomStorageEnabled(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webview_settings.getSettings().setAppCachePath(appCachePath);
        webview_settings.getSettings().setAllowFileAccess(true);
        webview_settings.getSettings().setAppCacheEnabled(true);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
            webview_settings.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        //将一个java类对象传递到HTML页面中，
        webview_settings.addJavascriptInterface(this, "JSBridge");
        webview_settings.setWebChromeClient(new WebChromeClient());
        webview_settings.setDownloadListener(new DownloadListener(){
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                        String mimetype, long contentLength) {
                Log.e("HEHE","开始下载");
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });
        webview_settings.loadUrl("file:///android_asset/www/settings.html");

    }
    //-----------------------------------------------------------
//显示信息
    @JavascriptInterface
    public void showlocalData() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if( !rwSPdata("r","jw_account","").isEmpty()){
                    String jw_account = rwSPdata("r","jw_account","");
                    webview_settings.loadUrl("javascript:set_jw_account('"+jw_account+"');");
                }

                if( !rwSPdata("r","xxmh_account","").isEmpty()){
                    String xxmh_account= rwSPdata("r","xxmh_account","");
                    webview_settings.loadUrl("javascript:set_xxmh_account('"+xxmh_account+"');");
                }
            }
        });
    }
//教务系统 点击登陆
    @JavascriptInterface
    public void denglu_jiaowu() {//js->原生
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Intent intent=new Intent(page_settings.this, page_login.class);
                    intent.putExtra("extra_data","教务系统登陆");
                    startActivity(intent);
                }catch (Exception e){
                    Toast.makeText(page_settings.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
//教务系统 退出登陆
    @JavascriptInterface
    public void tuichu_jiaowu() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rwSPdata("w","jw_account","");
                rwSPdata("w","jw_passwd", "");
                rwSPdata("w","userdata","");
                //重载页面
                webview_settings.loadUrl("javascript:window.location.reload();");

            }
        });
    }
    //信息门户 点击登陆
    @JavascriptInterface
    public void denglu_xxmh() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Intent intent=new Intent(page_settings.this, page_login_xxmh.class);
                    intent.putExtra("extra_data","信息门户登陆");
                    startActivity(intent);
                }catch (Exception e){
                    Toast.makeText(page_settings.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
//信息门户 退出登陆
    @JavascriptInterface
    public void tuichu_xxmh() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                rwSPdata("w","xxmh_account","");
                rwSPdata("w","xxmh_passwd", "");
                //重载页面
                webview_settings.loadUrl("javascript:window.location.reload();");

            }
        });
    }
//检查更新
    @JavascriptInterface
    public void getAppVersion() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webview_settings.loadUrl("javascript:checkupdate('"+getAppVersionCode(page_settings.this)+"','"+BuildConfig.VERSION_NAME+"')");
            }
        });
    }
//关于
    @JavascriptInterface
    public void guanyupage() {//js->原生，
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(page_settings.this, page_loadurl.class);
                intent.putExtra("extra_data","https://gitee.com/xyxywan/hfut_education");
                startActivity(intent);

            }
        });
    }

    @JavascriptInterface
    public void getUserdata() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //读取本地SD里储存的账号密码，向服务器请求信息
                if(rwSPdata("r","islogin","").contains("True")){
                    String account = rwSPdata("r","jw_account","");
                    String passwd = rwSPdata("r","jw_passwd", "");
                    String data_url ="getcourse?name="+account+"&passwd="+passwd;
                    webview_settings.loadUrl("javascript:java_savedata('"+data_url+"')");
                }else {
                    Toast.makeText(page_settings.this, "登陆" , Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(page_settings.this, page_login.class);
                    intent.putExtra("extra_data","教务系统登陆");
                    startActivity(intent);
                }
            }
        });
    }

    public String rwSPdata(String flag,String key,String value){
        //email,passwd,vrcode
        SharedPreferences testSP= getSharedPreferences("testSP", Activity.MODE_PRIVATE);
        switch (flag){
            case "w":
                SharedPreferences.Editor editor = testSP.edit();
                editor.putString( key, value );
                editor.apply();
                return "";
            case "r":
                return testSP.getString( key, "" );
            default:
                return "";
        }
    }



    public static long getAppVersionCode(Context context) {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("", e.getMessage());
        }
        return appVersionCode;
    }




}